<?php

class DbTrack extends Zend_Db_Table_Row_Abstract
{
    /**
     * Pro rychlejsi vyhledavani se pouziva md5 hash z nazvu songu
     *
     * @param Track $track
     * @return string
     */
    public static function getSearchKey(Track $track)
    {
        return substr(md5($track->getSongName() . $track->getArtist()->getName()), 0, 8);
    }

    /**
     *
     * @param Track $track
     * @return integer
     */
    public static function findTrack(Track $track)
    {

        $db = Zend_Db_Table::getDefaultAdapter();

        $select = $db->select()->from('tracks', 'id');
        $select->where('code = ?', self::getSearchKey($track));

        $track_id = $db->fetchOne($select);

        return $track_id;
    }

    /**
     *
     * @param array $data JSON
     */
    public static function insertTrack(array $data, $track = null)
    {

        if (is_null($track))
        {
            $track = new Track($data['SongName'], $data['ArtistName']);
        }

        $db = Zend_Db_Table::getDefaultAdapter();

        $db->beginTransaction();


        $track_id = $data['SongID'];

        $select = $db->select()->from('artists', new Zend_Db_Expr('COUNT(*)'))->where('id = ?', $data['ArtistID']);
        $artist_exists = (bool) $db->fetchOne($select);

        if (!$artist_exists)
        {
            $set = array();
            $set['id'] = $data['ArtistID'];
            $set['name'] = $data['ArtistName'];
            $db->insert('artists', $set);
        }

        $set = array();
        $set['code'] = self::getSearchKey($track);
        $set['id'] = $data['SongID'];
        $set['song'] = $data['SongName'];
        $set['album'] = $data['AlbumName'];
        $set['url'] = $data['Url'];
        $set['artist_id'] = $data['ArtistID'];
        $set['artist_name'] = $data['ArtistName'];
        $set['inserted'] = new Zend_Db_Expr('NOW()');


        // maybe song name was modified so the key is obsolete
        $db->delete('tracks', $db->quoteInto('id = ?', $data['SongID']));
        $db->insert('tracks', $set);
        $db->commit();
    }

    /**
     *
     * @param Track $track
     * @return DbTrack
     */
    public static function loadTrack(Track $track)
    {

        $track_id = self::findTrack($track);


        // podivame se, jestli mame track v DB

        if (empty($track_id))
        {
            // track neni v DB, zkusime ziskat data z grooveshark
            $data = TinySong::request($track->getName());

            if (empty($data))
            {
                // no result from grooveshark
                // we are FUCKED

                return null;
            }

            self::insertTrack($data, $track);
        }


        if (!empty($track_id))
        {
            $tracks = new DbTracks();

            $track = $tracks->find($track_id)->current();

            return $track;
        }

        return null;
    }

    /**
     *
     * @return string
     */
    public function getSong()
    {
        return $this->song;
    }

    /**
     *
     * @return integer
     */
    public function getID()
    {
        return $this->id;
    }

    /**
     *
     * @return string
     */
    public function getAlbum()
    {
        return $this->album;
    }

    /**
     *
     * @return string
     */
    public function getUrl()
    {
        return $this->url;
    }

    /**
     *
     * @return DbArtist
     */
    public function getArtist()
    {
        return $this->findParentRow('DbArtists');
    }

    /**
     *
     * @return string
     */
    public function getName()
    {
        return $this->artist_name . ' - ' . $this->getSong();
    }

    /**
     *
     * @return string
     */
    public function __toString()
    {
        return $this->getName();
    }

}
